package com.example.dense_rack_server.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.example.dense_rack_server.entity.Notice;
import com.example.dense_rack_server.service.NoticeService;
import com.example.dense_rack_server.utils.ResponseModel;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author 张彬
 * 通知控制器
 */
@Controller
@RequestMapping("/notice")
public class NoticeController {

    @Resource
    public NoticeService noticeService;


    @GetMapping("/index")
    public String list() {
        return "notice/index";
    }


    /**
     * 获取通知列表
     */
    @GetMapping("/list")
    @ResponseBody
    public Map<String, Object> listTable(@RequestParam(value = "hostId", required = false) String hostId,
                                         @RequestParam(value = "page") int page,
                                         @RequestParam(value = "limit") int limit) {
        return noticeService.listTable(hostId, page, limit);
    }

    /**
     * 通知修改
     */
    @GetMapping("/edit")
    public ModelAndView edit(@RequestParam(value = "noticeId") Integer noticeId) {
        Notice notice = noticeService.getById(noticeId);
        ModelAndView modelAndView = new ModelAndView("notice/edit");
        modelAndView.addObject("noticeId", noticeId);
        modelAndView.addObject("noticeContent", notice.getNoticeContent());

        return modelAndView;
    }


    /**
     * 通知保存
     */
    @PostMapping("/update/save")
    @ResponseBody
    public ResponseModel updateSave(@RequestBody JSONObject data) throws InterruptedException {
        if (StrUtil.isEmpty(data.getStr("noticeContent")) || StrUtil.isEmpty(data.getStr("noticeId"))) {
            return ResponseModel.fail("参数错误");
        }
        return noticeService.updateSave(data.getInt("noticeId"), data.getStr("noticeContent"),data.getInt("syncFunction"));
    }


}
